using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FetCo.Data;
using ADESSA.Business;
using ADESSA.Entities;
using FetCo.Framework;
using FetCo.Web;

public partial class Admin_Category : System.Web.UI.Page
{
    #region Variable
    protected long Id
    {
        get
        {
            object o = ViewState["Id"];
            if (o != null)
                return (long)o;
            return long.MinValue;
        }
        set
        {
            ViewState["Id"] = value;
        }
    }
    public int NoCountStart
    {
        get { return Convert.ToInt32(ViewState["NoCountStart"]); }
        set { ViewState["NoCountStart"] = value; }
    }
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {

        if (IsPostBack)
            return;
        if (Request.QueryString["id"] != null)
        {
            this.Id = long.Parse(Request.QueryString["id"].ToString());
        }
        LoadData();
        LoadCurent();

    }
    #region LoadCurent
    private void LoadCurent()
    {
        HtmlGenericControl ulArticle = (HtmlGenericControl)Master.FindControl("ulArticle");
        ulArticle.Attributes.Add("class", "current");
        HtmlGenericControl liSubShowCategory = (HtmlGenericControl)Master.FindControl("liSubShowCategory");
        liSubShowCategory.Attributes.Add("class", "sub_show");
        HtmlControl divSelectArticle = (HtmlControl)Master.FindControl("divSelectArticle");
        divSelectArticle.Attributes.Add("class", "select_sub show");
    }
    #endregion
    #region LoadData

    private void LoadData()
    {
        Criteria cri = new Criteria();
        ArticleCatCollection _ArticleCatCollection = ArticleCatService.GetArticleCatList(ArticleCatColumns.Order, OrderDirection.ASC);
        rptList.DataSource = _ArticleCatCollection;
        rptList.DataBind();
        ADESSA.Entities.ArticleCat _ArticleCat = new ADESSA.Entities.ArticleCat();
        if (this.Id != long.MinValue && this.Id != 0)
        {
            _ArticleCat = ArticleCatService.GetArticleCat(this.Id);
            if (_ArticleCat != null)
            {
                txtTitle.Value = _ArticleCat.Name;
                txtCode.Value = _ArticleCat.Code;
                txtOrder.Value = _ArticleCat.Order.ToString();
            }
        }


    }
    protected void rptList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            ArticleCat _ArticleCat = (ArticleCat)e.Item.DataItem;
            TextBox txtOrder = (TextBox)e.Item.FindControl("txtOrder");
            HiddenField hidID = (HiddenField)e.Item.FindControl("hidID");
            HtmlTableRow tr = (HtmlTableRow)e.Item.FindControl("tr");
            hidID.Value = _ArticleCat.CategoryID.ToString();
            int STT = NoCountStart + e.Item.ItemIndex;
            txtOrder.Text = _ArticleCat.Order.ToString();
            if (STT % 2 != 0)
            {
                tr.Attributes["class"] = "alternate-row";
            }
            else
            {
                tr.Attributes["class"] = "none";

            }
        }
    }

    #endregion
    #region Create And Update
    protected void linkSave_Click(object sender, EventArgs e)
    {
        ADESSA.Entities.ArticleCat _ArticleCat = new ADESSA.Entities.ArticleCat();
        if (this.Id != long.MinValue && this.Id != 0)
        {
            _ArticleCat = ArticleCatService.GetArticleCat(this.Id);
        }
       
        _ArticleCat.Name = txtTitle.Value.Trim();
        _ArticleCat.Code = txtCode.Value.Trim();
        if (!string.IsNullOrEmpty(txtOrder.Value.Trim()))
            _ArticleCat.Order = int.Parse(txtOrder.Value.Trim());
        else
            _ArticleCat.Order = RTISettings.DefaultOrder;

        if (this.Id != long.MinValue && this.Id != 0)
        {

            ArticleCatService.UpdateArticleCat(_ArticleCat);
            Response.Redirect("Category.aspx");

        }
        else
        {
            if (checkGroupNameAddnew(txtCode.Value.Trim()))
            {
                lb_error.Text = "Not duplicate code";
                return;
            }
            ArticleCatService.CreateArticleCat(_ArticleCat);
            Response.Redirect("Category.aspx");
        }
    }
    #endregion
    #region Delete and Check Code
    protected void lndelete_Click(object sender, EventArgs e)
    {
        foreach (RepeaterItem item in this.rptList.Items)
        {
            CheckBox chkDelete = (CheckBox)item.FindControl("chkDelete");
            if (chkDelete.Checked == true)
            {
                HiddenField hidID = (HiddenField)item.FindControl("hidID");
                string strhidID = hidID.Value;
                long ID = long.Parse(strhidID);

                try
                {
                    #region Check Order data
                    Criteria cri = new Criteria();
                    cri.add(ArticleSubCatColumns.CategoryID, ID, Condition.EQUAL, Condition.AND);
                    ArticleSubCatCollection _ArticleSubCatCollection = ArticleSubCatService.GetArticleSubCatListByParam(ArticleSubCatColumns.CategoryID, OrderDirection.ASC, cri.Criter);
                    if (_ArticleSubCatCollection.Count > 0)
                    {
                        lb_error.Text = "Dont's delete this item because it is getting using another items";
                    }
                    else
                    {
                        ArticleCatService.DeleteArticleCat(ID);
                    }
                    #endregion
                }
                catch
                {
                    lb_error.Text = "Dont's delete this item because it is getting using another items";
                }
            }
        }
        LoadData();
    }
    private bool checkGroupNameAddnew(string Code)
    {
        bool kq = false;
        Criteria cri = new Criteria();
        cri.add("[" + ArticleCat._Table + "].[" + ArticleCatColumns.Code + "]", Code, Condition.EQUAL, Condition.AND);
        ArticleCatCollection _ArticleCatCollection = ArticleCatService.GetArticleCatListByParam("[" + ArticleCat._Table + "].[" + ArticleCatColumns.Order + "]", OrderDirection.ASC, cri.Criter);
        if (_ArticleCatCollection.Count >= 1)
        {
            kq = true;
        }
        return kq;
    }
    #endregion
    #region Update for Order
    protected void btnSave_OnClick(object sender, EventArgs e)
    {
        foreach (RepeaterItem item in this.rptList.Items)
        {
            HiddenField hidID = (HiddenField)item.FindControl("hidID");
            CheckBox ckEnable = (CheckBox)item.FindControl("ckEnable");
            ArticleCat _ArticleCat = ArticleCatService.GetArticleCat(long.Parse(hidID.Value));
            TextBox txtOrder = (TextBox)item.FindControl("txtOrder");

            if (_ArticleCat != null)
            {
                _ArticleCat.Order = int.Parse(txtOrder.Text.Trim());
                ArticleCatService.UpdateArticleCat(_ArticleCat);
            }
        }
        LoadData();
    }
    #endregion

}
